#include "../include/muduo/proto/dispatcher.h"
#include "../include/muduo/proto/codec.h"
#include "../include/muduo/base/Logging.h"
#include "../include/muduo/base/Mutex.h"
#include "../include/muduo/net/EventLoop.h"
#include "../include/muduo/net/TcpClient.h"
#include "../include/muduo/net/EventLoopThread.h"
#include "../include/muduo/base/CountDownLatch.h"

#include "request.pb.h"
#include <iostream>

class Client
{
public:
    typedef std::shared_ptr<google::protobuf::Message> MessagePtr;
    typedef std::shared_ptr<ppr::TranslateResponse> TranslateResponsePtr;
    typedef std::shared_ptr<ppr::AddResponse> AddResponsePtr;
    Client(const std::string &sip, int sport)
        : _latch(1), _client(_loopthread.startLoop(), muduo::net::InetAddress(sip, sport), "Client"),
          _dispatcher(std::bind(&Client::onUnknownMessage, this,
                                std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)),
          _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher,
                           std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    {
        //注册业务请求处理函数
        _dispatcher.registerMessageCallback<ppr::TranslateResponse>(std::bind(&Client::onTranslate,this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<ppr::AddResponse>(std::bind(&Client::onAdd,this,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::onConnection, this,std::placeholders::_1));
    }
    // 业务处理函数
    void translate(const std::string& meg)
    {
        ppr::TranslateRequest req; 
        req.set_meg(meg);
        send(&req);
    }

    void add(int num1,int num2)
    {
        ppr::AddRequest req;
        req.set_num1(num1);
        req.set_num2(num2);
        send(&req);
    }
    void connect()
    {
        _client.connect();
        _latch.wait();
    }
private:
    bool send(const google::protobuf::Message *message)
    {
        if(_conn->connected())
        {
            _codec.send(_conn,*message);
            return true;
        }
        return false;
    }

    void onConnection(const muduo::net::TcpConnectionPtr& conn)
    {
        if(conn->connected())
        {
            _conn = conn;
            _latch.countDown();//唤醒主线程中的阻塞
        }
        else
        {
            _conn.reset();
        }
    }

    void onTranslate(const muduo::net::TcpConnectionPtr &conn, const TranslateResponsePtr &message, muduo::Timestamp)
    {
        std::cout<<"翻译结果:"<<message->meg()<<std::endl;
    }

    void onAdd(const muduo::net::TcpConnectionPtr &conn, const AddResponsePtr &message, muduo::Timestamp)
    {
        std::cout<<"计算结果:"<<message->result()<<std::endl;
    }

    void onUnknownMessage(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

private:
    muduo::CountDownLatch _latch;            // 保证同步关系
    muduo::net::EventLoopThread _loopthread; // 事件循环线程，内部包含一个EventLoop对象 负责处理客户端的网络事件
    muduo::net::TcpConnectionPtr _conn;      // TCP 连接智能指针，保存与服务器的连接对象
    muduo::net::TcpClient _client;           // TCP 客户端对象，用于与服务器建立连接和收发数据
    ProtobufDispatcher _dispatcher;          // 请求分发器
    ProtobufCodec _codec;                    // 协议处理器
};

int main()
{
    Client client("127.0.0.1",8080);
    client.connect();

    client.translate("hello");
    client.add(11,22);

    sleep(1);
    return 0;
}